<?
$sub_menu = "400100";
include_once("./_common.php");

$UPLOAD_DIR = $g4[path]."/data/goods/";

$g_sell_price		= $_POST['g_sell_price'] ? $_POST['g_sell_price'] : 0;
$g_qty					= $_POST['g_qty'] ? $_POST['g_qty'] : 1;
$g_show					= $_POST['g_show'] ? $_POST['g_show'] : 1;
$g_order_flag   = $_POST['g_order_flag'] ? $_POST['g_order_flag'] : 1;
$g_order				= $_POST['g_order'] ? $_POST['g_order'] : 0;


$_SQL = "	g_sell_price	= '" . $g_sell_price . "' ,
					g_qty					= '" . $g_qty . "' ,
					g_show				= '" . $g_show . "',
					g_order_flag	= '" . $g_order_flag . "',
					g_icon				= '" . $_POST['g_icon1'] . "^^" . $_POST['g_icon2'] . "^^" . $_POST['g_icon3'] . "^^" . $_POST['g_icon4'] . "',
					g_order				= '" . $g_order . "' ";


function img_resize($tmpname, $size, $save_dir, $save_name, $maxisheight = 0)
{
	$save_dir.= ( substr($save_dir,-1) != "/") ? "/" : "";
	$gis = getimagesize($tmpname);
	$type = $gis[2];
	switch($type)
	{
		case "1": $imorig = imagecreatefromgif($tmpname); break;
		case "2": $imorig = imagecreatefromjpeg($tmpname);break;
		case "3": $imorig = imagecreatefrompng($tmpname); break;
		default:  $imorig = imagecreatefromjpeg($tmpname);
	}

	$x = imagesx($imorig);
	$y = imagesy($imorig);

	$woh = (!$maxisheight)? $gis[0] : $gis[1] ;

	if($woh <= $size)
	{
		$aw = $x;
		$ah = $y;
	}
	else
	{
		if(!$maxisheight)
		{
			$aw = $size;
			$ah = $size;
		}
		else
		{
			$aw = $size;
			$ah = $size;
		}
	}
	$im = imagecreatetruecolor($aw,$ah);
	if(imagecopyresampled($im,$imorig , 0,0,0,0,$aw,$ah,$x,$y))
	{
		if(imagejpeg($im, $save_dir.$save_name))
		{
			return $save_name;
		}
		else
		{
			return "";
		}
	}
}


if(empty($w))
{
	auth_check($auth[$sub_menu], "w");


	$g_price = str_replace(",","",$g_price);
	$g_cnt = str_replace(",","",$g_cnt);
	$g_min = str_replace(",","",$g_min);
	$g_max = str_replace(",","",$g_max);
	$g_content = addslashes($g_content);

	if($autosize == "Y")
	{

		if($_FILES['g_img3']['tmp_name']!="")
		{
			$tmpname  = $_FILES['g_img3']['tmp_name'];
			$G_IMG1 = @img_resize( $tmpname , 100 , $UPLOAD_DIR , $g_code."_1.jpg");
			$G_IMG2 = @img_resize( $tmpname , 300 , $UPLOAD_DIR , $g_code."_2.jpg");
			$G_IMG3 = @img_resize( $tmpname , 500 , $UPLOAD_DIR , $g_code."_3.jpg");
			@unlink($tmpname);
		}
	}
	else
	{
		if($_FILES['g_img1']['tmp_name']!="")
		{
			$tmpname1  = $_FILES['g_img1']['tmp_name'];
			$G_IMG1 = @img_resize( $tmpname1 , 100 , $UPLOAD_DIR , $g_code."_1.jpg");
			@unlink($tmpname1);
		}

		if($_FILES['g_img2']['tmp_name']!="")
		{
			$tmpname2  = $_FILES['g_img2']['tmp_name'];
			$G_IMG2 = @img_resize( $tmpname2 , 300 , $UPLOAD_DIR , $g_code."_2.jpg");
			@unlink($tmpname2);
		}

		if($_FILES['g_img3']['tmp_name']!="")
		{
			$tmpname3  = $_FILES['g_img3']['tmp_name'];
			$G_IMG3 = @img_resize( $tmpname3 , 500 , $UPLOAD_DIR , $g_code."_3.jpg");
			@unlink($tmpname3);
		}
	}

	$tmp_g_option = array();
	if(count($g_option_name) > 0)
	{
		for($i=0;$i<count($g_option_name);$i++)
		{
			if($g_option_name[$i]!="")
			{
				$tmp_option = $g_option_name[$i]."[FIELD]";
				if(trim($g_option_price[$i]) == "")
				{
					$tmp_option.= "0";
				}
				else
				{
					$tmp_option.= str_replace(",","",$g_option_price[$i]);
				}
				$tmp_g_option[] = $tmp_option;
			}
		}
	}
	$g_option = implode("[RECORD]",$tmp_g_option);


	$sql = "INSERT INTO " . $g4['goods_table'] . "
						SET c_code		= '". $c_code . "',
								g_code		= '". $g_code . "',
								g_name		= '". $g_name . "',
								g_info		= '". $g_info . "',
								g_create	= '". $g_create ."',
								g_nation	= '". $g_nation ."',
								g_price		= '". $g_price ."',
								g_option	= '". $g_option ."',
								g_cnt			= '". $g_cnt ."',
								g_min			= '". $g_min ."',
								g_max			= '". $g_max ."',
								g_content = '". $g_content ."',
								g_sale		= '0',
								g_hit			= '0',
								g_img1		= '". $G_IMG1 ."',
								g_img2		= '". $G_IMG2 ."',
								g_img3		= '". $G_IMG3 ."',
								g_tranc		= '". $g_tranc ."', "
								. $_SQL . ",
								g_datetime = now() ";

	sql_query($sql);
}
elseif($w == "UPDATE")
{
	auth_check($auth[$sub_menu], "w");

	$g_price = str_replace(",","",$g_price);
	$g_cnt = str_replace(",","",$g_cnt);
	$g_min = str_replace(",","",$g_min);
	$g_max = str_replace(",","",$g_max);
	$g_content = addslashes($g_content);

	$DATA = mysql_fetch_array(sql_query("select * from g4_goods where idx='$IDX' limit 1"));


	if($autosize == "Y")
	{

		if($_FILES['g_img3']['tmp_name']!="")
		{
			@unlink($UPLOAD_DIR.$DATA[g_img1]);
			@unlink($UPLOAD_DIR.$DATA[g_img2]);
			@unlink($UPLOAD_DIR.$DATA[g_img3]);

			$tmpname  = $_FILES['g_img3']['tmp_name'];
			$G_IMG1 = @img_resize( $tmpname , 100 , $UPLOAD_DIR , $g_code."_1.jpg");
			$G_IMG2 = @img_resize( $tmpname , 300 , $UPLOAD_DIR , $g_code."_2.jpg");
			$G_IMG3 = @img_resize( $tmpname , 500 , $UPLOAD_DIR , $g_code."_3.jpg");
			@unlink($tmpname);
		}
		else
		{
			$G_IMG1 = $DATA[g_img1];
			$G_IMG2 = $DATA[g_img2];
			$G_IMG3 = $DATA[g_img3];
		}
	}
	else
	{
		if($_FILES['g_img1']['tmp_name']!="")
		{
			@unlink($UPLOAD_DIR.$DATA[g_img1]);
			$tmpname1  = $_FILES['g_img1']['tmp_name'];
			$G_IMG1 = @img_resize( $tmpname1 , 100 , $UPLOAD_DIR , $g_code."_1.jpg");
			@unlink($tmpname1);
		}
		else
		{
			if($g_img1_del=="YES")
			{
				@unlink($UPLOAD_DIR.$DATA[g_img1]);
			}
			else
			{
				$G_IMG1 = $DATA[g_img1];
			}
		}

		if($_FILES['g_img2']['tmp_name']!="")
		{
			@unlink($UPLOAD_DIR.$DATA[g_img2]);
			$tmpname2  = $_FILES['g_img2']['tmp_name'];
			$G_IMG2 = @img_resize( $tmpname2 , 300 , $UPLOAD_DIR , $g_code."_2.jpg");
			@unlink($tmpname2);
		}
		else
		{
			if($g_img2_del=="YES")
			{
				@unlink($UPLOAD_DIR.$DATA[g_img2]);
			}
			else
			{
				$G_IMG2 = $DATA[g_img2];
			}
		}

		if($_FILES['g_img3']['tmp_name']!="")
		{
			@unlink($UPLOAD_DIR.$DATA[g_img3]);
			$tmpname3  = $_FILES['g_img3']['tmp_name'];
			$G_IMG3 = @img_resize( $tmpname3 , 500 , $UPLOAD_DIR , $g_code."_3.jpg");
			@unlink($tmpname3);
		}
		else
		{
			if($g_img3_del=="YES")
			{
				@unlink($UPLOAD_DIR.$DATA[g_img3]);
			}
			else
			{
				$G_IMG3 = $DATA[g_img3];
			}
		}
	}

	$tmp_g_option = array();
	if(count($g_option_name) > 0)
	{
		for($i=0;$i<count($g_option_name);$i++)
		{
			if($g_option_name[$i]!="")
			{
				$tmp_option = $g_option_name[$i]."[FIELD]";
				if(trim($g_option_price[$i]) == "")
				{
					$tmp_option.= "0";
				}
				else
				{
					$tmp_option.= str_replace(",","",$g_option_price[$i]);
				}
				$tmp_g_option[] = $tmp_option;
			}
		}
	}
	$g_option = implode("[RECORD]",$tmp_g_option);


	$sql = "update g4_goods set ";
	$sql.= "c_code = '".$c_code."', ";
	$sql.= "g_code = '".$g_code."', ";
	$sql.= "g_name = '".$g_name."', ";
	$sql.= "g_info = '".$g_info."', ";
	$sql.= "g_create = '".$g_create."', ";
	$sql.= "g_nation = '".$g_nation."', ";
	$sql.= "g_price = '".$g_price."', ";
	$sql.= "g_option = '".$g_option."', ";
	$sql.= "g_cnt = '".$g_cnt."', ";
	$sql.= "g_min = '".$g_min."', ";
	$sql.= "g_max = '".$g_max."', ";
	$sql.= "g_content = '".$g_content."', ";
	$sql.= "g_img1 = '".$G_IMG1."', ";
	$sql.= "g_img2 = '".$G_IMG2."', ";
	$sql.= "g_img3 = '".$G_IMG3."', ";
	$sql.= "g_tranc = '".$g_tranc."', ";
	$sql.= $_SQL;
	$sql.= "where idx='$IDX' limit 1";
	sql_query($sql);
}
elseif($w == "DELETE")
{
	auth_check($auth[$sub_menu], "d");
	$DATA = mysql_fetch_array(sql_query("select * from g4_goods where idx='$IDX' limit 1"));

	@unlink($UPLOAD_DIR.$DATA[g_img1]);
	@unlink($UPLOAD_DIR.$DATA[g_img2]);
	@unlink($UPLOAD_DIR.$DATA[g_img3]);

	$sql = "delete from g4_goods where idx='$IDX' limit 1";
	sql_query($sql);
}
elseif($w == "SDEL")
{
	auth_check($auth[$sub_menu], "d");
	for($i=0;$i<count($sidx);$i++)
	{
		$DATA = mysql_fetch_array(sql_query("select * from g4_goods where idx='".$sidx[$i]."' limit 1"));

		@unlink($UPLOAD_DIR.$DATA[g_img1]);
		@unlink($UPLOAD_DIR.$DATA[g_img2]);
		@unlink($UPLOAD_DIR.$DATA[g_img3]);

		$sql = "delete from g4_goods where idx='".$sidx[$i]."' limit 1";
		sql_query($sql);
	}
}

goto_url("./goods_list.php");
?>